package com.team7.happycommunity.propertymanagement.service.impl;

import com.team7.happycommunity.propertymanagement.dao.OwnerParkingApplyMapper;
import com.team7.happycommunity.propertymanagement.dao.ParkingInfoMapper;
import com.team7.happycommunity.propertymanagement.dao.PropertyManageMapper;
import com.team7.happycommunity.propertymanagement.pojo.OwnerParkingApply;
import com.team7.happycommunity.propertymanagement.pojo.ParkingInfo;
import com.team7.happycommunity.propertymanagement.pojo.PropertyManage;
import com.team7.happycommunity.propertymanagement.service.OwnerParkingApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;

import java.util.List;

@Component
public class OwnerParkingApplyServiceImpl implements OwnerParkingApplyService {

    @Autowired
    OwnerParkingApplyMapper ownerParkingApplyMapper;

    @Autowired
    PropertyManageMapper propertyManageMapper;

    @Autowired
    ParkingInfoMapper parkingInfoMapper;

    @Override
    public List<OwnerParkingApply> getList(Integer currentPage, Integer pageSize, String username) {
        PropertyManage propertyManage = propertyManageMapper.selectByUserName(username);
//        ;
        return ownerParkingApplyMapper.getAll(currentPage,pageSize,propertyManage.getVillageId());
    }

    @Override
    public int refuse(Integer id) {
        OwnerParkingApply ownerParkingApply = new OwnerParkingApply();
        ownerParkingApply.setId(id);
        ownerParkingApply.setStatus(2); // 2 是未通过
//        ownerParkingApplyMapper.updateByPrimaryKeySelective(ownerParkingApply);
        return ownerParkingApplyMapper.updateByPrimaryKeySelective(ownerParkingApply);
    }

    @Override
    public int pass(Integer id) {
        OwnerParkingApply ownerParkingApply = new OwnerParkingApply();
        ownerParkingApply.setId(id);
        ownerParkingApply.setStatus(1); // 2 是未通过
        OwnerParkingApply ownerParkingApplyold = ownerParkingApplyMapper.selectByPrimaryKey(id);
        // 创建新的车位信息
        ParkingInfo parkingInfo = new ParkingInfo();
        parkingInfo.setStatus(0);
        parkingInfo.setType(1);
        parkingInfo.setOwnerId(ownerParkingApplyold.getOwnerId());
        parkingInfo.setCommunityId(ownerParkingApplyold.getCommunityId());
        parkingInfo.setBeginTime(ownerParkingApplyold.getBeginTime());
        parkingInfo.setEndTime(ownerParkingApplyold.getEndTime());
        parkingInfo.setCostPerDay(ownerParkingApplyold.getCostPerDay());
        parkingInfo.setParkingNumber(ownerParkingApplyold.getParkingCode());
        parkingInfo.setParkingInfos(ownerParkingApplyold.getParkingInfo());
        parkingInfoMapper.insertSelective(parkingInfo);

        return ownerParkingApplyMapper.updateByPrimaryKeySelective(ownerParkingApply);
    }
}
